package org.jeecg.common.view;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecg.common.entity.ExportParamsExEntity;
import org.jeecg.common.export.ExcelExportUtil;
import org.jeecgframework.poi.excel.def.MapExcelConstants;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.view.MiniAbstractExcelView;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author gchiaway
 * 日期: 2021-09-02
 * 时间: 10:33
 */
@SuppressWarnings("unchecked")
@Controller("mapExcelView")
public class MapExcelView extends MiniAbstractExcelView {

    public MapExcelView() {
        super();
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Expose-Headers", "content-disposition,result-status");
        Boolean success = (Boolean) model.get("success");
        if (!success) {
            String message = (String) model.get("message");
            String code = (String) model.get("code");
            if (isIE(request)) {
                message = java.net.URLEncoder.encode(message, "UTF8");
                code = java.net.URLEncoder.encode(code, "UTF8");
            } else {
                message = new String(message.getBytes("UTF-8"), "ISO-8859-1");
                code = new String(code.getBytes("UTF-8"), "ISO-8859-1");
            }
            response.setHeader("result-status", "success=" + false + ";message=" + message + ";code=" + code);
            return;
        }
        String codedFileName = "临时文件";
        Workbook workbook = ExcelExportUtil.exportExcel((ExportParamsExEntity) model.get(MapExcelConstants.PARAMS), (List<ExcelExportEntity>) model.get(MapExcelConstants.ENTITY_LIST), (Collection<? extends Map<?, ?>>) model.get(MapExcelConstants.MAP_LIST));
        if (model.containsKey(MapExcelConstants.FILE_NAME)) {
            codedFileName = (String) model.get(MapExcelConstants.FILE_NAME);
        }
        String extensionName;
        if (workbook instanceof HSSFWorkbook) {
            codedFileName += HSSF;
            extensionName = HSSF;
        } else {
            codedFileName += XSSF;
            extensionName = XSSF;
        }
        if (isIE(request)) {
            codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");
            extensionName = java.net.URLEncoder.encode(extensionName, "UTF8");
        } else {
            codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
            extensionName = new String(extensionName.getBytes("UTF-8"), "ISO-8859-1");
        }
        response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ";extensionname=" + extensionName);
        response.setHeader("result-status", "success=true");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
    }

}
